package mest.service.Modeling.Impl;

import com.alibaba.fastjson.JSON;
import mest.dao.Modeling.IapartFileDao;
import mest.entity.apart_file;
import mest.service.Modeling.IapartFileService;
import mest.util.mestUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @auther Machenike
 * @create 2019-11-15 21:21
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class apartFileServiceImpl implements IapartFileService {


    @Resource
    private IapartFileDao iafd;

    @Override
    public String insertapartFile(apart_file ct,String theirDb)  {
       /* try {
            return iafd.insertapartFile(ct, theirDb);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;*/

        Map<String,Object> map = new HashMap<String,Object>();
        try {
            iafd.insertapartFile(ct, theirDb);
            map.put("code",0);
            map.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getClass().getName().equals("org.springframework.dao.DuplicateKeyException")) {
                map.put("msg","编码重复");
                return JSON.toJSONString(map);
            }
            map.put("msg", "添加失败");
        }
        return JSON.toJSONString(map);
    }



    @Override
    public int selectCount(String theirDb) {
        return iafd.selectCount(theirDb);
    }

    @Override
    public List<apart_file> findApartFilePage(int index, int pageSize,String theirDb) {
        return iafd.findApartFilePage((index-1)*pageSize,pageSize, theirDb);
    }


    //删除方法
    @Override
    public int delModel(Integer id,String theirDb) {
        return iafd.delModel(id,theirDb);
    }

    @Override
    public List<apart_file> queryAll(Integer index, Integer pageSize, String record,String theirDb) {
        return iafd.queryAll((index-1)*pageSize, pageSize, record,theirDb);
    }

    @Override
    public int findCount(String record,String theirDb) {
        return iafd.findCount(record, theirDb);
    }


    @Override
    public String updateByOne(apart_file record,String theirDb) {
        Map<String,Object> map = new HashMap<String,Object>();
        try {
            iafd.updateByPrimaryKeySelective(record,theirDb);
            map.put("code",0);
            map.put("msg","操作成功");
        }catch (Exception e){
            e.printStackTrace();
            if (e.getClass().getName().equals("org.springframework.dao.DuplicateKeyException")) {
                map.put("msg","编码重复");
                return JSON.toJSONString(map);
            }
            map.put("msg","操作失败");
        }
        return JSON.toJSONString(map);
    }

}
